package com.example.experiment03.Analysisservce.IMPL;




import com.example.experiment03.CODData.Coddata;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@Slf4j
@Service
@EnableScheduling
public class CodDataserviceImpl implements InitializingBean {
    public static Resource body;
    public String URL = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";

    @Scheduled(cron = "0 0 2 * * *")
    @Override
    public void afterPropertiesSet() throws Exception {
        RequestEntity<Void> requestEntity = RequestEntity.get(URI.create(URL))
                .headers(httpHeaders -> httpHeaders.add("User-Agent", "Mozilla/5.0"))
                .build();
        ResponseEntity<Resource> exchange = new RestTemplate().exchange(requestEntity, Resource.class);
        body = exchange.getBody();
    }

    public List<Coddata> getCodData() {
        List<Coddata> coddataList = new ArrayList<>();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = body.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            bufferedReader = new BufferedReader(inputStreamReader);

            Iterable<CSVRecord> csvRecord = CSVFormat.DEFAULT.withHeader("Province/State", "Country/Region", "Lat", "Long").parse(bufferedReader);
            Iterator<CSVRecord> iterator = csvRecord.iterator();
            iterator.next();

            while (iterator.hasNext()) {
                CSVRecord record = iterator.next();

                Coddata CodDataModel = new Coddata();
                CodDataModel.setCountry(record.get("Country/Region"));
                CodDataModel.setState(record.get("Province/State"));
                CodDataModel.setDiffFromPervDay(Integer.parseInt(record.get(record.size() - 1)) - Integer.parseInt(record.get(record.size() - 2)));
                CodDataModel.setLatestTotalCases(Integer.parseInt(record.get(record.size() - 1)));
                coddataList.add(CodDataModel);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return coddataList;
        }
    }
}



